<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-10
 * Time: 16:25
 */

namespace app\manage\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder; // 引入ZBuilder
use app\common\model\Rank as RankModel;
use think\Db;

class Rank extends Admin
{

    //公告列表
    public function index()
    {
        // 读取数据
        $map = $this->getMap();
        // 排序
        $order = $this->getOrder();
        // 数据列表
        $btn_access = [
            'title' => '新增会员折扣',
            'icon' => 'fa fa-fw fa-plus',
            'class' => 'btn btn-info',
            'href' => url('add'),
        ];
        $data_list = RankModel::where($map)
            ->order($order)
            ->paginate()
            ->each(function ($item){
                if($item['type']=='2'){
                    $item['validity']='余额不为0有效';
                }
                return  $item;
            });
        // 分页数据
        $page = $data_list->render();

        $btn_access1 = [
            'title' => '编辑会员折扣',
            'icon' => 'fa fa-fw fa-pencil',
            'class' => 'btn btn-xs btn-default',
            'href' => url('edit', ['id' => '__id__']),
        ];
        $btn_access2 = [
            'title' => '删除会员折扣',
            'icon' => 'fa fa-fw fa-trash-o',
            'class' => 'btn btn-xs btn-default',
            'href' => url('delete', ['id' => '__id__']),
        ];
        //设置页面
        return ZBuilder::make('table')
            ->hideCheckbox()
            ->addColumn('id', '编号', 'text')
            ->addColumn('rank_name', '等级名称', 'text')
            ->addColumn('price', '价格', 'text')
            ->addColumn('validity', '有效期', 'text')
            ->addColumn('type', '状态', 'status', '', [1 => '时效会员:success', 2 => '余额会员:danger'])
            ->addColumn('right_button', '操作', 'btn')
            ->setTableName('Rank')// 指定数据表名
            ->addTopButton('add', $btn_access)// 添加顶部按钮
            ->addRightButton('edit', $btn_access1)// 添加右侧按钮
            ->addRightButton('delete', $btn_access2)// 添加右侧按钮
            ->setPrimaryKey('id')// 设置主键名为username
            ->setPageTitle('会员折扣列表')
            ->setRowList($data_list)// 设置表格数据
            ->setPages($page)// 设置分页数据
            ->fetch();
    }

    //添加食堂
    public function add()
    {
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            $result = $this->validate($data, 'Rank');
            if (true !== $result) $this->error($result);
            if ($page = RankModel::create($data)) {
                $this->success('新增成功', 'index');
            } else {
                $this->error('新增失败');
            }
        } else {
            return ZBuilder::make('form')
                ->addFormItems(
                    [
                        ['text:7', 'rank_name', '级别名称'],
                        ['text:7', 'price', '价格'],
                        ['text:7', 'validity', '有效期','<code>单位为年</code>'],
                        ['radio', 'type', '状态', '', [1 => '时效会员', 2 => '余额会员'], '1']
                    ]
                )
                ->setTrigger('type','1','validity')
                ->setPageTitle('添加会员折扣')
                ->fetch();
        }
    }

    //编辑食堂
    public function edit($id = null)
    {
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Rank');
            if (true !== $result) $this->error($result);
            if (RankModel::update($data)) {
                $this->success('编辑成功', 'index');
            } else {
                $this->error('编辑失败');
            }
        } else {
            $info = RankModel::get($id);
            return ZBuilder::make('form')
                ->addFormItems(
                    [
                        ['hidden', 'id', 'id'],
                        ['text:7', 'rank_name', '级别名称'],
                        ['text:7', 'price', '价格'],
                        ['text:7', 'validity', '有效期'],
                        ['radio', 'type', '状态', '', [1 => '时效会员', 2 => '余额会员'], '1']
                    ]
                )
                ->setTrigger('type','1','validity')
                ->setFormData($info)
                ->setPageTitle('编辑套餐')
                ->fetch();
        }
    }

    //删除公告
    public function delete($id = null)
    {
        if ($id === null) $this->error('缺少参数');
        $re = RankModel::destroy($id);//
        if ($re) {
            $this->success('删除成功', 'index');
        } else {
            $this->error('系统繁忙请稍后再试', 'index');
        }

    }
}